# This is an pre-build script.  It is run before epp parses anything.

# This script makes a dark-style page for any page that uses the
# default template tag.

import os

EPP_EXT = '.epp'
TEMPLATE_TAG = '[default_template'
DARK_STYLE_NAME_PREFIX = 'dark_'
DARK_STYLE_PREPEND = '''
[define replace default_css_includes:]
    [base_address]/css/dark_style.css
'''

for root, dirs, files in os.walk(os.getcwd()):
    for f in files:
        if f.endswith(EPP_EXT) and not f.startswith(DARK_STYLE_NAME_PREFIX):
            infname = os.path.join(root, f)
            inf = open(infname, 'r')
            instr = inf.read()
            inf.close()
            if TEMPLATE_TAG in instr:
                darkfname = os.path.join(root, DARK_STYLE_NAME_PREFIX + f)
                darkf = open(darkfname, 'w')
                darkf.write(DARK_STYLE_PREPEND)
                darkf.write(instr)
                darkf.close()
                print("applied dark styling to %r: %r"
                        % (f, DARK_STYLE_NAME_PREFIX + f))